/*
 * File Name  : DemoAction.java
 * Author        : wodenwang
 * Date             : 2012-12-11
 */

package com.wodensoft.module;

import static com.wodensoft.core.web.Actions.includePage;
import static com.wodensoft.core.web.Actions.redirectInfoPage;

import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.h2.message.DbException;

import com.wodensoft.core.db.DataCondition;
import com.wodensoft.core.db.DataPackage;
import com.wodensoft.core.db.ORMService;
import com.wodensoft.core.db.ORMServiceTest;
import com.wodensoft.core.db.SessionFactoryHelper;
import com.wodensoft.core.web.Actions.Util;

public class DemoAction {

	public DemoAction() {
		// 模拟动态注册（表不存在时建表）Demo对象
		SessionFactoryHelper.Config conf = new SessionFactoryHelper.Config();
		conf.addInputStream(ORMServiceTest.class.getResourceAsStream("/hbm/DEMO.hbm.xml"));
		SessionFactoryHelper.refresh(conf);
	}

	/**
	 * demo汇总
	 * 
	 * @param request
	 * @param response
	 * @throws WebException
	 * @throws DbException
	 */
	public void index(HttpServletRequest request, HttpServletResponse response) {
		includePage(request, response, Util.getPagePath(request, "main.jsp"));
	}

	/**
	 * 消息展示
	 * 
	 * @param request
	 * @param response
	 * @throws WebException
	 * @throws DbException
	 * @throws InterruptedException
	 */
	public void msg(HttpServletRequest request, HttpServletResponse response) throws InterruptedException {

		TimeUnit.SECONDS.sleep(5);// 等待5秒看loading
		redirectInfoPage(request, response, "ajax测试成功。");
	}

	/**
	 * table列表
	 * 
	 * @param request
	 * @param response
	 * @throws WebException
	 * @throws DbException
	 */
	public void list(HttpServletRequest request, HttpServletResponse response) {
		// 获取分页信息
		int start = Util.getStart(request);
		int limit = Util.getLimit(request);
		// 获取排序信息
		String field = Util.getSortField(request);
		String dir = Util.getSortDir(request);

		// 随便查询一些什么出来
		// 查询条件
		DataCondition condition = new DataCondition().setNumberBig("id", "22").setOrderByDesc("id");
		condition.setOrderBy(field, dir);

		// 查询Demo实例（DEMO表）
		DataPackage dp = ORMService.getInstance().queryPackage("Demo", start, limit, condition.toEntry());

		// 设置到页面
		request.setAttribute("dp", dp);

		includePage(request, response, Util.getPagePath(request, "list.jsp"));
	}

	/**
	 * 表单
	 * 
	 * @param request
	 * @param response
	 * @throws WebException
	 */
	public void form(HttpServletRequest request, HttpServletResponse response) {
		includePage(request, response, Util.getPagePath(request, "form.jsp"));
	}

	/**
	 * 布局
	 * 
	 * @param request
	 * @param response
	 * @throws WebException
	 */
	public void layout(HttpServletRequest request, HttpServletResponse response) {
		includePage(request, response, Util.getPagePath(request, "layout.jsp"));
	}
}
